#ifndef P_SCENE_H
#define P_SCENE_H

#include "PUUID.h"
#include "entt/entity/registry.hpp"
#include "System/PSystem.h"

namespace Piccolo
{
    class PEntity;
    class PNode;

    class PScene
    {
    public:
        void OnInit();
        void OnDestroy();

        PEntity* CreateEntity(const std::string& name = "");
        PEntity* CreateEntityWithUUID(const PUUID& id, const std::string& name = "");
        void DestroyEntity(const PEntity* entity);
        void DestroyAllEntity();

        template<typename T, typename... Args>
        void AddSystem(Args&&... args)
        {
            mSystemList.push_back(std::make_shared<T>(std::forward<Args>(args)...));
        }

        void UpdateSystemList(float deltaTime)
        {
            for (auto& item : mSystemList)
            {
                item->OnTick(deltaTime);
            }
        }

        entt::registry& GetEcsRegistry() { return mEcsRegistry; }
        PNode* GetRootNode() { return mRootNode.get(); }
        PEntity* GetEntity(entt::entity enttEntity);

    private:
        friend class PEntity;

        std::string mName;
        entt::registry mEcsRegistry;

        std::unordered_map<entt::entity, std::shared_ptr<PEntity>> mEntities;
        std::shared_ptr<PNode> mRootNode;
        PNode* mPickedNode = nullptr;

        std::vector<std::shared_ptr<PSystem>> mSystemList;
    };
}

#endif